import * as THREE from 'three';
import PointsMaterial from "./PointsMaterial"
import Points from './Points';
export default class PointCloud extends THREE.EventDispatcher {
  scene;
  annotate3D;
  annotate2D = [];
  groupPoints;
  groupTrack;
  selection;
  selectionMap;
  renderViews;
  ground;
  trimBox;
  pixelRatio;
  // loading
  loading = false;
  timeStamp = 0;
  //
  material = new PointsMaterial();
  selectColor = new THREE.Color(1, 0, 0);
  highlightColor = new THREE.Color(1, 0, 0);
  renderTimer = 0;

  constructor() {
    super();
    this.pixelRatio = 1;
    // this.pixelRatio = window.devicePixelRatio;
    this.scene = new THREE.Scene();
    // this.scene.autoUpdate = false;
    this.annotate3D = new THREE.Group();
    this.groupPoints = new THREE.Group();
    this.groupTrack = new THREE.Group();

    let ground = new THREE.PlaneHelper(
      new THREE.Plane(new THREE.Vector3(0, 0, -1), 0),
      100,
      0xcccccc,
    );
    this.ground = ground;
    this.ground.visible = false;

    let box = new THREE.Box3(
      new THREE.Vector3(-100, -100, -100),
      new THREE.Vector3(100, 100, 100),
    );
    this.trimBox = new THREE.Box3Helper(box, new THREE.Color(0xffff00));
    this.trimBox.visible = false;

    this.scene.add(
      this.groupPoints,
      this.annotate3D,
      this.ground,
      this.trimBox,
      this.groupTrack,
    );

    const axesHelper = new THREE.AxesHelper(100);
    axesHelper.visible = false;
    this.scene.add(axesHelper);

    this.selection = [];
    this.selectionMap = {};
    this.renderViews = [];

    // test
    // this.initStats();

    // @ts-ignore
    // window.pc = this;
  }

  // initTween() {
  //   requestAnimationFrame(animate);

  //   function animate(time) {
  //     requestAnimationFrame(animate);
  //     TWEEN.update(time);
  //   }
  // }
  addRenderView(view) {
    console.log('执行了addRenderView=============》PointCloud');
    if (this.renderViews.indexOf(view) > 0) return;
    
    view.init();
    this.renderViews.push(view);
  }

  removeRenderView(view) {
    let index = this.renderViews.indexOf(view);
    if (index < 0) return;

    this.renderViews.splice(index, 1);
    view.destroy();
  }

  setPointCloudData(data) {
    let points;
    if (this.groupPoints.children.length === 0) {
      points = new Points(this.material);
      this.groupPoints.add(points)
    } else {
      points = this.groupPoints.children[0];
    }
    points.updateData(data);
  }
}